<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		
	<span class="different-posts different-posts_earlier">📖 <a href="/page/13">earlier posts</a> 📖</span>




	<article>
	
		<h1><a href="/2017/10/12/Shell/4. shell 结构以及执行/">shell 结构以及执行</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell/">shell</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;写第一个 shell 脚本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># cd /usr/local/sbin</span></div><div class="line">[root@192 sbin]<span class="comment"># vim first.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">#this is my first script.</span></div><div class="line"><span class="comment">#written by yanyi.</span></div><div class="line">date</div><div class="line"><span class="built_in">echo</span> <span class="string">"Hello woeld!"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;shell 脚本通常都是以 .sh 为后缀的。这个并不是说不带 .sh 这个脚本就不能执行，只是大家的一个习惯。所以，以后发现了 .sh 为后缀的文件那么它可能是一个 shell 脚本。first.sh 中第一行要以 “<strong>#!/bin/bash</strong>” 开头，代表的意思是，该文件使用的是 bash 语法。如果不设置该行，虽然 shell 脚本也可以执行，但是不符合规范。如果把这个脚本放到一个默认 shell 并非 bash 的系统里，那么这个脚本很有可能是不能成功执行的，因为 bash 和其他的 shell 不兼容。 # 表示注释，这个符号后面跟一些该脚本的相关注释内容以及作者和创建日期或者版本等。当然这些注释并非必须的，也可以省略掉，但是不建议省略。因为随着工作时间的逐渐过渡，很有肯能忘记该脚本是用来干什么的，以及什么时候写的。所有写上注释是有必要的。另外，系统管理员并非只有有一个人，如果是其他管理员查看脚本，看不懂会很郁闷。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行脚本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh first.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E7%BB%93%E6%9E%84%E4%BB%A5%E5%8F%8A%E6%89%A7%E8%A1%8C/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其实 shell 脚本还有另一种执行方法：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># ./first.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E7%BB%93%E6%9E%84%E4%BB%A5%E5%8F%8A%E6%89%A7%E8%A1%8C/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;想要使用另一种方法运行 shell 脚本，前提是脚本本身有执行权限。所以，需要给脚本加一个 x 权限。另外使用 sh 命令去执行一个 shell 脚本的时候是可以加 -x 选项来查看这个脚本执行过程的，这样有利于我们调试这个脚本哪里出了问题：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh -x first.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E7%BB%93%E6%9E%84%E4%BB%A5%E5%8F%8A%E6%89%A7%E8%A1%8C/03.png?raw=true" alt=""></figure></p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell/3. shell 中的特殊符号/">shell 中的特殊符号</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell/">shell</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;shell中常用到的特殊字符。</p>
<ul>
<li>* 代表零个或多个任意字符</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@localhost 111]<span class="comment"># ls</span></div><div class="line">1 11.txt 1.txt 22.txt 2.txt 33.txt 3.txt 44.zip 55.zip</div><div class="line">[root@localhost 111]<span class="comment"># ls *.txt</span></div><div class="line">11.txt 1.txt 22.txt 2.txt 33.txt 3.txt</div></pre></td></tr></table></figure>
<ul>
<li>？ 只代表一个任意的字符</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@localhost 111]<span class="comment"># ls</span></div><div class="line">1 11.txt 1.txt 22.txt 2.txt 33.txt 3.txt 44.zip 55.zip</div><div class="line">[root@localhost 111]<span class="comment"># ls ?.txt</span></div><div class="line">1.txt 2.txt 3.txt</div></pre></td></tr></table></figure>
<ul>
<li># 注释符号</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个符号在linux中表示注释说明的意思，即#后面的内容linux忽略掉。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># abc=123 #aaa</span></div><div class="line">[root@localhost ~]<span class="comment"># echo $abc</span></div><div class="line">123</div></pre></td></tr></table></figure>
<ul>
<li>\ 脱意字符<br>  它将后面的特殊符号（例如“*”）还原为普通字符。</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ls -d test\*</span></div><div class="line">ls: 无法访问<span class="built_in">test</span>*: 没有那个文件或目录</div></pre></td></tr></table></figure>
<ul>
<li>| 管道符</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;它的作用在于将符号前面命令的结果丢给符号后面的命令。这里提到的后面命令，并不是所有的命令都可以的，一般针对文档操作的命令比较常用，例如 cat , less , head , tail , greo , cut , sort , wc , unip , tee , tr , split , sed , awk 等等，其中grep ，sed ，awk 为正则表达式。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cat /etc/passwd|wc -l</span></div><div class="line">21</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;wc -l用来计算一个文档有多少行。</p>
<ul>
<li>特殊符号 $</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;$ 除了用于变量前面的标识符外，还有一个妙用，就是和“!”结合起来使用。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ls 1.py</span></div><div class="line">1.py</div><div class="line">[root@localhost ~]<span class="comment"># ls !$</span></div><div class="line">ls 1.py</div><div class="line">1.py</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;!$ 表示上条命令中最后一个变量（总之就是上条命令中最后出现的那个东西）例如上边命令最后是 1.py 那么在当前命令下输入 !$ 则代表 1.py</p>
<ul>
<li>特殊符号 ；</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;平时在一行中敲一个命令，然后回车就运行了，那么在一行中运行两个或两个以上的命令则需要在命令之间加一个“；”</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ls *.py;touch 4.py;ls *.py</span></div><div class="line">1.py  2.py  3.py</div><div class="line">1.py  2.py  3.py  4.py</div></pre></td></tr></table></figure>
<ul>
<li>特殊符号 ~</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用户的家目录。如果是root则是/root。普通用户则是/home/username</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cd ~</span></div><div class="line">[root@localhost ~]<span class="comment"># pwd</span></div><div class="line">/root</div><div class="line">[root@localhost ~]<span class="comment"># su test</span></div><div class="line">[<span class="built_in">test</span>@localhost root]$ <span class="built_in">cd</span> ~</div><div class="line">[<span class="built_in">test</span>@localhost ~]<span class="comment"># pwd</span></div><div class="line">/home/<span class="built_in">test</span></div></pre></td></tr></table></figure>
<ul>
<li>特殊符号 &amp;</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果想把一条命令放到后台执行的话，则需要加上“&amp;”这个符号。通常用于时间非常长的情况。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># sleep 100 &amp;</span></div><div class="line">[2] 2393</div><div class="line">[root@localhost ~]<span class="comment"># jobs</span></div><div class="line">[1]+  运行中               sleep 100 &amp;</div></pre></td></tr></table></figure>
<ul>
<li>重定向符号 &gt; &gt;&gt; 2&gt; 2&gt;&gt;</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重定向符号&gt;以及&gt;&gt;,分别表示取代和追加的意思。然后还有两个符号就是2&gt;和2&gt;&gt;，分别表示错误重定向和错误追加重定向。当运行一个命令报错时，报错信息会输出到当前的屏幕，如果想重定向到一个文本里，则要用2&gt;或者2&gt;&gt;。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ls aaaa</span></div><div class="line">ls: 无法访问aaaa: 没有那个文件或目录</div><div class="line">[root@localhost ~]<span class="comment"># ls aaaa 2&gt; /tmp/error</span></div><div class="line">[root@localhost ~]<span class="comment"># cat /tmp/error</span></div><div class="line">ls: 无法访问aaaa: 没有那个文件或目录</div><div class="line">[root@localhost ~]<span class="comment"># ls aaaa 2&gt;&gt; /tmp/errror</span></div><div class="line">[root@localhost ~]<span class="comment"># cat /tmp/error</span></div><div class="line">ls: 无法访问aaaa: 没有那个文件或目录</div></pre></td></tr></table></figure>
<ul>
<li>中括号 []</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;中间为字符组合，代表中间字符中的任意一个。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ls *.py</span></div><div class="line">1.py  2.py  3.py  4.py</div><div class="line">[root@localhost ~]<span class="comment"># ls [1-3].py</span></div><div class="line">1.py  2.py  3.py</div><div class="line">[root@localhost ~]<span class="comment"># ls [0-9].py</span></div><div class="line">1.py  2.py  3.py  4.py</div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell/2. shell 特性/">shell 特性</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell/">shell</a>
			</span>
		
	</div>

	

	
		<h2 id="命令历史"><a href="#命令历史" class="headerlink" title="命令历史"></a>命令历史</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;敲过的命令，Linux 是会有记录的，预设可以记录1000条历史命令。这些命令保存在用户的家目录中的 <code>.bash-history</code> 文件中。只有当用户正常退出当前的 shell 时，在当前 shell 中运行的命令才会保存至 .bash_history 文件中。</p>
<h3 id="环境变量"><a href="#环境变量" class="headerlink" title="环境变量"></a>环境变量</h3><ul>
<li><strong>HISTSIZE</strong>：命令历史记录的条数；</li>
<li><strong>HISTFILE</strong>：~/.bash_history</li>
<li><strong>HISTFILESIZE</strong>：命令历史文件记录历史的条数</li>
<li><strong>HISTTIMEFORMAT</strong>：命令的时间戳。这个环境变量需设置，这个功能只能在这个变量被设置之后，那些新执行的 bash 命令才会被打上正确的时间戳。在此之前的所有命令，都将会显示成设置 HISTTIMEFORMAT 变量的时间。</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设置 <code>HISTTIMEFORMAT</code></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">export</span> HISTTIMEFORMAT=<span class="string">'%F %T'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看历史命令的时间戳</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">history</span> | more</div><div class="line">1 2008-08-05 19:02:39 service network restart</div><div class="line">2 2008-08-05 19:02:39 <span class="built_in">exit</span></div><div class="line">3 2008-08-05 19:02:39 id</div><div class="line">4 2008-08-05 19:02:39 cat /etc/redhat-release</div></pre></td></tr></table></figure>
<h3 id="history-参数"><a href="#history-参数" class="headerlink" title="history 参数"></a>history 参数</h3><ul>
<li>history [n]  n为数字，列出最近的n条命令</li>
<li>-c  将目前shell中的所有history命令消除</li>
<li>history [-raw] histfiles<ul>
<li>-a 将目前新增的命令写入histfiles, 默认写入~/.bash_history</li>
<li>-r  将histfiles内容读入到目前shell的history记忆中</li>
<li>-w 将目前history记忆的内容写入到histfiles</li>
</ul>
</li>
</ul>
<h3 id="调用历史中的命令"><a href="#调用历史中的命令" class="headerlink" title="调用历史中的命令"></a>调用历史中的命令</h3><h4 id="命令："><a href="#命令：" class="headerlink" title="命令：!!"></a>命令：!!</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;两个连续的 “!”，表示执行上一条命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># pwd</span></div><div class="line">/root</div><div class="line">[root@localhost ~]<span class="comment"># !!</span></div><div class="line"><span class="built_in">pwd</span></div><div class="line">/root</div></pre></td></tr></table></figure>
<h4 id="命令：-n"><a href="#命令：-n" class="headerlink" title="命令：!n"></a>命令：!n</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;”!n”，这里的 n 是数字，表示执行命令历史中第 n 条指令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># history</span></div><div class="line"></div><div class="line">.......</div><div class="line"></div><div class="line">1058 vim .bashrc</div><div class="line">1059 <span class="built_in">pwd</span></div><div class="line">1060 <span class="built_in">history</span></div><div class="line"></div><div class="line">[root@localhost ~]<span class="comment"># !1059</span></div><div class="line"><span class="built_in">pwd</span></div><div class="line">/root</div></pre></td></tr></table></figure>
<h4 id="命令：”-字符串”"><a href="#命令：”-字符串”" class="headerlink" title="命令：”!字符串”"></a>命令：”!字符串”</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;”!字符串”（字符串大于等于1），以 p 为例，执行命令历史中最近一次 p 开头的命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># !p</span></div><div class="line"><span class="built_in">pwd</span></div><div class="line">/root</div></pre></td></tr></table></figure>
<h2 id="别名-alias"><a href="#别名-alias" class="headerlink" title="别名 alias"></a>别名 alias</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;别名 alias 也是 bash 所特有的功能之一。可以通过 alias 把一个常用的并且很长的指令别名成简介易记的指令。不想用了还可以用 unalias 解除别名功能。直接敲 alias 会看到目前系统预设的 alias</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># alias</span></div><div class="line"><span class="built_in">alias</span> cp=<span class="string">'cp -i'</span></div><div class="line"><span class="built_in">alias</span> egrep=<span class="string">'egrep --color=auto'</span></div><div class="line"><span class="built_in">alias</span> fgrep=<span class="string">'fgrep --color=auto'</span></div><div class="line"><span class="built_in">alias</span> grep=<span class="string">'grep --color=auto'</span></div><div class="line"><span class="built_in">alias</span> l.=<span class="string">'ls -d .* --color=auto'</span></div><div class="line"><span class="built_in">alias</span> ll=<span class="string">'ls -l --color=auto'</span></div><div class="line"><span class="built_in">alias</span> ls=<span class="string">'ls --color=auto'</span></div><div class="line"><span class="built_in">alias</span> mv=<span class="string">'mv -i'</span></div><div class="line"><span class="built_in">alias</span> rm=<span class="string">'rm -i'</span></div><div class="line"><span class="built_in">alias</span> <span class="built_in">which</span>=<span class="string">'alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'</span></div></pre></td></tr></table></figure>
<h3 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">alias</span> [命令别名]=[<span class="string">'具体的命令'</span>]</div></pre></td></tr></table></figure>
<h3 id="命令格式"><a href="#命令格式" class="headerlink" title="命令格式"></a>命令格式</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;给命令自定义一个名字，当前 shell 有效</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># alias denny='pwd'</span></div><div class="line">[root@localhost ~]<span class="comment"># denny</span></div><div class="line">/root</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;取消命令别名</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># unalias denny</span></div><div class="line">[root@localhost ~]<span class="comment"># denny</span></div><div class="line">-bash: denny: <span class="built_in">command</span> not found</div></pre></td></tr></table></figure>
<h2 id="通配符"><a href="#通配符" class="headerlink" title="通配符"></a>通配符</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;“*” 匹配零个或多个字符</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@localhost 111]<span class="comment"># ls</span></div><div class="line">1 11.txt 1.txt 22.txt 2.txt 33.txt 3.txt 44.zip 55.zip</div><div class="line">[root@localhost 111]<span class="comment"># ls *.txt</span></div><div class="line">11.txt 1.txt 22.txt 2.txt 33.txt 3.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;“？”匹配一个字符</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@localhost 111]<span class="comment"># ls</span></div><div class="line">1 11.txt 1.txt 22.txt 2.txt 33.txt 3.txt 44.zip 55.zip</div><div class="line">[root@localhost 111]<span class="comment"># ls ?.txt</span></div><div class="line">1.txt 2.txt 3.txt</div></pre></td></tr></table></figure>
<h2 id="输入输出重定向（-gt-gt-gt-lt-2-gt-2-gt-gt-）"><a href="#输入输出重定向（-gt-gt-gt-lt-2-gt-2-gt-gt-）" class="headerlink" title="输入输出重定向（&gt;,&gt;&gt;,&lt;,2&gt;,2&gt;&gt;）"></a>输入输出重定向（&gt;,&gt;&gt;,&lt;,2&gt;,2&gt;&gt;）</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输入重定向用于改变命令的输入，输出重定向用于改变命令的输出。输出重定向更为常用，经常用于将命令的结果输入到文件中，而不是屏幕上。输入重定向的命令是 &lt;，输出重定向是命令是 &gt;，另外还有追加重定向&gt;&gt;，以及错误重定向2&gt;。</p>
<h3 id="命令：-gt"><a href="#命令：-gt" class="headerlink" title="命令：&gt;"></a>命令：&gt;</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输出重定向，把文件里的内容删掉，写入新的。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost 111]<span class="comment"># cat 1.txt</span></div><div class="line">1111</div><div class="line">[root@localhost 111]<span class="comment"># echo '222'&gt;1.txt</span></div><div class="line">[root@localhost 111]<span class="comment"># cat 1.txt</span></div><div class="line">222</div></pre></td></tr></table></figure>
<h3 id="命令：-gt-gt"><a href="#命令：-gt-gt" class="headerlink" title="命令：&gt;&gt;"></a>命令：&gt;&gt;</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;追加重定向，在文件原来的基础上在下面继续写入。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@localhost 111]<span class="comment"># echo '333'&gt;&gt;1.txt</span></div><div class="line">[root@localhost 111]<span class="comment"># cat 1.txt</span></div><div class="line">222</div><div class="line">333</div></pre></td></tr></table></figure>
<h3 id="命令：-lt"><a href="#命令：-lt" class="headerlink" title="命令：&lt;"></a>命令：&lt;</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输入重定向，把文件的内容输入到前面</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost 111]<span class="comment"># wc -l&lt;1.txt</span></div><div class="line">2</div></pre></td></tr></table></figure>
<h3 id="命令：2-gt-2-gt-gt"><a href="#命令：2-gt-2-gt-gt" class="headerlink" title="命令：2&gt;,2&gt;&gt;"></a>命令：2&gt;,2&gt;&gt;</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;错误重定向；追加错误重定向。把错误的内容重定向或追加重定向到文件中去</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[root@localhost 111]<span class="comment"># ls 5 2&gt; 1.txt</span></div><div class="line">[root@localhost 111]<span class="comment"># cat 1.txt</span></div><div class="line">ls: 无法访问5: 没有那个文件或目录</div><div class="line"></div><div class="line">[root@localhost 111]<span class="comment"># ls 5 2&gt;&gt; 1.txt</span></div><div class="line">[root@localhost 111]<span class="comment"># cat 1.txt</span></div><div class="line">ls: 无法访问5: 没有那个文件或目录</div><div class="line">ls: 无法访问5: 没有那个文件或目录</div></pre></td></tr></table></figure>
<h2 id="管道符“-”"><a href="#管道符“-”" class="headerlink" title="管道符“|”"></a>管道符“|”</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;管道符“|”，就是把前面的命令运行的结果丢给后面的命令去处理</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cat 1.py|wc -l</span></div><div class="line">6</div><div class="line">[root@localhost ~]<span class="comment"># cat /etc/passwd|wc -l</span></div><div class="line">21</div></pre></td></tr></table></figure>
<h2 id="作业控制"><a href="#作业控制" class="headerlink" title="作业控制"></a>作业控制</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当运行一个进程时，可以使它暂停（ctrl+z），然后使用fg命令恢复它，利用bg命令使它到后台运行，也可以终止它（ctrl+c）。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用 vim 编辑 test1.txt 。随便输入一些内容，按“ESC”后，使用“ctrl+z”使任务暂停</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># vim test1.txt</span></div><div class="line"></div><div class="line">[1]+  已停止               vim test1.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以看到提示“vim test1.txt”已停止了，然后使用 fg 命令恢复它，此时就进入刚才的 vim 窗口了。使用“ctrl+c”直接终止 vim 任务。</p>
<h2 id="命令：jobs"><a href="#命令：jobs" class="headerlink" title="命令：jobs"></a>命令：jobs</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看暂停的进程</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># sleep 100</span></div><div class="line">^Z</div><div class="line">[2]+  已停止               sleep 100</div><div class="line">[root@localhost ~]<span class="comment"># jobs</span></div><div class="line">[1]-  已停止               vim test1.txt</div><div class="line">[2]+  已停止               sleep 100</div></pre></td></tr></table></figure>
<h2 id="命令-fg；fg-编号"><a href="#命令-fg；fg-编号" class="headerlink" title="命令 fg；fg [编号]"></a>命令 fg；fg [编号]</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;恢复暂停进程，标号后面带加号的优先恢复，减号其次，也可以恢复指定暂停中的进程</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># jobs</span></div><div class="line">[1]-  已停止               vim test1.txt</div><div class="line">[2]+  已停止               sleep 100</div><div class="line">[root@localhost ~]<span class="comment"># fg 1</span></div><div class="line">[1]+  已停止               vim test1.txt</div><div class="line">[root@localhost ~]<span class="comment"># jobs</span></div><div class="line">[1]+  已停止               vim test1.txt</div><div class="line">[2]-  已停止               sleep 100</div></pre></td></tr></table></figure>
<h2 id="命令：bg；bg-编号"><a href="#命令：bg；bg-编号" class="headerlink" title="命令：bg；bg [编号]"></a>命令：bg；bg [编号]</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;后台运行暂停的进程，编号后面带加号的有限后台运行，减号其次，也可以指定后台运行暂停中的某个进程，&amp; 表示后台运行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># jobs</span></div><div class="line">[1]+  已停止               vim test1.txt</div><div class="line">[2]-  已停止               sleep 100</div><div class="line">[root@localhost ~]<span class="comment"># bg 2</span></div><div class="line">[2]- sleep 100 &amp;</div><div class="line">[root@localhost ~]<span class="comment"># jobs</span></div><div class="line">[1]+  已停止               vim test1.txt</div><div class="line">[2]-  完成                  sleep 100</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;丢到后台的任务如何关掉？如果没有退出刚才的 shell 那么先使用 “fg 编号”把任务调到前台，然后使用“ctrl+c”结束任务</p>
<h2 id="命令：ps"><a href="#命令：ps" class="headerlink" title="命令：ps"></a>命令：ps</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用 &amp; 把任务丢如后台运行，可以使用 <code>ps aux</code> 命令找到那个进程，结束该进程需要使用 kill 命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ps aux|grep vim</span></div><div class="line">root       2307  0.0  0.2 151464  4996 pts/0    T    11:35   0:00 vim test1.txt</div><div class="line">root       2349  0.0  0.0 112664   972 pts/0    S+   12:34   0:00 grep --color=auto vim</div></pre></td></tr></table></figure>
<h2 id="命令：kill"><a href="#命令：kill" class="headerlink" title="命令：kill"></a>命令：kill</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果关闭当前的 shell ，再次打开另一个 shell 时，使用 jobs 命令并不能显示后台运行或者被暂停的任务，要想停掉它们，则需要先知道起PID，然后使用 kill 命令杀死那个进程</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用 &amp; 把任务丢到后台运行，会显示 pid 信息。忘记这个 pid 使用 ps aux 命令查找</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># vmstat 1 &gt; /tmp/1.log &amp;</span></div><div class="line">[2] 2351</div><div class="line">[root@localhost ~]<span class="comment"># ps aux | grep vmstat</span></div><div class="line">root       2351  0.0  0.0 148308  1348 pts/0    S    12:38   0:00 vmstat 1</div><div class="line">root       2353  0.0  0.0 112664   972 pts/0    S+   12:38   0:00 grep --color=auto vmstat</div></pre></td></tr></table></figure>
<h3 id="kill-命令语法"><a href="#kill-命令语法" class="headerlink" title="kill 命令语法"></a>kill 命令语法</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;kill 命令语法很简单，直接在后面加上 pid 即可，如果遇到杀不死的进程可以在 kill 后面加一个选项</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">kill</span> -9 [pid]</div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell/18.shell中的select用法/">shell中的select用法</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell/">shell</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;select也是循环的一种，它比较适合用在用户选择的情况下。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;比如，我们有一个这样的需求，运行脚本后，让用户去选择数字，选择1，会运行w命令，选择2运行top命令，选择3运行free命令，选择4退出。脚本这样实现：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"></div><div class="line"><span class="built_in">echo</span> <span class="string">"Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"</span></div><div class="line"><span class="built_in">echo</span></div><div class="line">select <span class="built_in">command</span> <span class="keyword">in</span> w top free quit</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="keyword">case</span> <span class="variable">$command</span> <span class="keyword">in</span></div><div class="line">    w)</div><div class="line">        w</div><div class="line">        ;;</div><div class="line">    top)</div><div class="line">        top</div><div class="line">        ;;</div><div class="line">    free)</div><div class="line">        free</div><div class="line">        ;;</div><div class="line">    quit)</div><div class="line">        <span class="built_in">exit</span></div><div class="line">        ;;</div><div class="line">    *)</div><div class="line">        <span class="built_in">echo</span> <span class="string">"Please input a number:(1-4)."</span></div><div class="line">        ;;</div><div class="line">    <span class="keyword">esac</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行结果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">sh select.sh</div><div class="line">Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit</div><div class="line"></div><div class="line">1) w</div><div class="line">2) top</div><div class="line">3) free</div><div class="line">4) quit</div><div class="line"><span class="comment">#? 1</span></div><div class="line"> 16:06:58 up 109 days, 22:01,  1 user,  load average: 0.11, 0.05, 0.01</div><div class="line">USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT</div><div class="line">root     pts/0    222.128.156.84   16:05    0.00s  0.00s  0.00s w</div><div class="line"></div><div class="line"><span class="comment">#? 3</span></div><div class="line">             total       used       free     shared    buffers     cached</div><div class="line">Mem:       1020328     943736      76592          0      86840     263624</div><div class="line">-/+ buffers/cache:     593272     427056</div><div class="line">Swap:      2097144      44196    2052948</div><div class="line"><span class="comment">#?</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们发现，select会默认把序号对应的命令列出来，每次输入一个数字，则会执行相应的命令，命令执行完后并不会退出脚本。它还会继续让我们再次输如序号。序号前面的提示符，我们也是可以修改的，利用变量PS3即可，再次修改脚本如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">PS3=<span class="string">"Please select a number: "</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"</span></div><div class="line"><span class="built_in">echo</span></div><div class="line"></div><div class="line">select <span class="built_in">command</span> <span class="keyword">in</span> w top free quit</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="keyword">case</span> <span class="variable">$command</span> <span class="keyword">in</span></div><div class="line">    w)</div><div class="line">        w</div><div class="line">        ;;</div><div class="line">    top)</div><div class="line">        top</div><div class="line">        ;;</div><div class="line">    free)</div><div class="line">        free</div><div class="line">        ;;</div><div class="line">    quit)</div><div class="line">        <span class="built_in">exit</span></div><div class="line">        ;;</div><div class="line">    *)</div><div class="line">        <span class="built_in">echo</span> <span class="string">"Please input a number:(1-4)."</span></div><div class="line">    <span class="keyword">esac</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果想要脚本每次输入一个序号后就自动退出，则需要再次更改脚本如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">PS3=<span class="string">"Please select a number: "</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"</span></div><div class="line"><span class="built_in">echo</span></div><div class="line"></div><div class="line">select <span class="built_in">command</span> <span class="keyword">in</span> w top free quit</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="keyword">case</span> <span class="variable">$command</span> <span class="keyword">in</span></div><div class="line">    w)</div><div class="line">        w;<span class="built_in">exit</span></div><div class="line">        ;;</div><div class="line">    top)</div><div class="line">        top;<span class="built_in">exit</span></div><div class="line">        ;;</div><div class="line">    free)</div><div class="line">        free;<span class="built_in">exit</span></div><div class="line">        ;;</div><div class="line">    quit)</div><div class="line">        <span class="built_in">exit</span></div><div class="line">        ;;</div><div class="line">    *)</div><div class="line">        <span class="built_in">echo</span> <span class="string">"Please input a number:(1-4)."</span>;<span class="built_in">exit</span></div><div class="line">    <span class="keyword">esac</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell/16. shell 函数/">shell函数</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell/">shell</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;函数就是把一段代码整理到了一个小单元中，并给这个小单元起一个名字，当用到这段代码时，直接调用这个小单元的名字即可。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;格式：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">function</span> <span class="function"><span class="title">funame</span></span>() &#123;</div><div class="line">    <span class="built_in">command</span></div><div class="line">｝</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;函数必须要放在最前面。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim func1.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="function"><span class="title">input</span></span>() &#123;</div><div class="line"><span class="built_in">echo</span> <span class="variable">$1</span></div><div class="line">&#125;</div><div class="line">input aaa</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上面的脚本其实就是一个非常简单的函数，函数名字为 input ，它的作用就是输出参数 1 的内容。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh func1.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E5%87%BD%E6%95%B0/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再来看加法运算的函数：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim func2.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;写入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="function"><span class="title">sum</span></span>() &#123;</div><div class="line">s=$[<span class="variable">$1</span>+<span class="variable">$2</span>]</div><div class="line"><span class="built_in">echo</span> <span class="variable">$s</span></div><div class="line">&#125;</div><div class="line">sum 1 2</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：sum 为一个加法运算的函数， $1 和 $2 为第一个和第二个参数， sum 1 2  ，其实就是 1+2 ，最后 echo 出来它们的和。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面是一个稍微复杂的函数:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim func3.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="function"><span class="title">ip</span></span>() &#123;</div><div class="line">ifconfig |grep -A1 <span class="string">"<span class="variable">$1</span>"</span>|tail -l |awk <span class="string">'&#123;print $2&#125;'</span>|awk -F <span class="string">':'</span> <span class="string">'&#123;print $2&#125;'</span></div><div class="line">&#125;</div><div class="line"><span class="built_in">read</span> -p <span class="string">"Please input the eth name:"</span> e</div><div class="line">myip=`ip <span class="variable">$e</span>`</div><div class="line"><span class="built_in">echo</span> <span class="string">"<span class="variable">$e</span> address is <span class="variable">$myip</span>"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：ip 函数其实就是通过 grep 和 awk 把网卡的 ip 给截取出来，只不过这个网卡的名字是让用户子机动手输入的。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh func3.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E5%87%BD%E6%95%B0/02.png?raw=true" alt=""></figure></p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell/15. shell 中断继续退出/">shell 中断继续退出</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell/">shell</a>
			</span>
		
	</div>

	

	
		<h2 id="break-直接结束本层循环："><a href="#break-直接结束本层循环：" class="headerlink" title="break 直接结束本层循环："></a>break 直接结束本层循环：</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim break.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 5`</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$i</span></div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$i</span>==3 ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        <span class="built_in">break</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$i</span></div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="built_in">echo</span> aaaaaaa</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行结果：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh break.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E4%B8%AD%E6%96%AD%E7%BB%A7%E7%BB%AD%E9%80%80%E5%87%BA/01.png?raw=true" alt=""></figure></p>
<h2 id="continue-忽略-continue-之下的代码，直接进行下一次循环："><a href="#continue-忽略-continue-之下的代码，直接进行下一次循环：" class="headerlink" title="continue 忽略 continue 之下的代码，直接进行下一次循环："></a>continue 忽略 continue 之下的代码，直接进行下一次循环：</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim continue.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 5`</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$i</span></div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$i</span> == 3 ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        <span class="built_in">continue</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$i</span></div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="built_in">echo</span> <span class="variable">$i</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行结果：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh continue.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E4%B8%AD%E6%96%AD%E7%BB%A7%E7%BB%AD%E9%80%80%E5%87%BA/02.png?raw=true" alt=""></figure></p>
<h2 id="exit-直接退出-shell："><a href="#exit-直接退出-shell：" class="headerlink" title="exit 直接退出 shell："></a>exit 直接退出 shell：</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim exit.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 5`</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$i</span></div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$i</span> == 3 ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        <span class="built_in">exit</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$i</span></div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="built_in">echo</span> aaaaaaa</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行结果：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh exit.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E4%B8%AD%E6%96%AD%E7%BB%A7%E7%BB%AD%E9%80%80%E5%87%BA/03.png?raw=true" alt=""></figure></p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell/14. while 循环/">while 循环</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell/">shell</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;while 循环常常用来写死循环的脚本，用于监控某项服务。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim while.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">a=5</div><div class="line"><span class="keyword">while</span> [ <span class="variable">$a</span> -ge 1 ]</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$a</span></div><div class="line">    a=$[<span class="variable">$a</span>-1]</div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;while 循环格式也很简单：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">while</span> 条件：<span class="keyword">do</span></div><div class="line">    <span class="built_in">command</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上例脚本的执行结果为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh while.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;另外可以把循环条件拿一个冒号代替，这样可以做到死循环，监控脚本常常这样写：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">while</span> :;<span class="keyword">do</span></div><div class="line">    <span class="built_in">command</span></div><div class="line">    sleep 3</div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面用 while 死循环，来写一个判断系统负载的脚本。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim 6.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="keyword">while</span> :;<span class="keyword">do</span></div><div class="line">    load=`uptime |awk <span class="string">'&#123;print $(NF-2)&#125;'</span>|cut -d. -f1`</div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$load</span> -gt 10 ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        <span class="built_in">echo</span> <span class="string">"system load is high."</span>|mail -s <span class="string">"system load"</span> 89429541@qq.com</div><div class="line">    <span class="keyword">fi</span></div><div class="line">    sleep 10</div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：uptime 命令是用来查看系统负载的，用 awk 截取倒数第三段，也即是平均 1 分钟的系统负载，然后只取整数部分。如果系统负载高于 10 则发邮件告警。每隔 10 秒检查一次。但是如果系统负载一直高于 10，那么将会出现每隔 10 秒发一次邮件的窘境。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell/13. for 循环/">for 循环</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell/">shell</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;shell 脚本中也算是一门简易的编程语言了，当然循环是不能缺少的。常用到的循环有 for 循环和 while 循环。for 循环的结构是在日常运维中使用最频繁的循环结构。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一个简单的 for 循环脚本：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim for.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 5`</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$i</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;脚本中的 seq 1 5 表示从 1 到 5 的一个序列。可以直接运行这个命令试一下。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;脚本执行结果：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh for.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/for%20%E5%BE%AA%E7%8E%AF/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;通过这个脚本就可以看到 for 循环的基本结构：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">for</span> 变量名 <span class="keyword">in</span> 循环的条件；<span class="keyword">do</span></div><div class="line">    <span class="built_in">command</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里的“循环的条件”可以写成一组字符串或者数字（用 1 个或者多个空格隔开），也可以是一条命令的执行结果：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># for i in 1 2 3 a b;do echo $i;done</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/for%20%E5%BE%AA%E7%8E%AF/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以写引用系统命令的执行结果，就像那个 seq 1 5 但是需要用反引号括起来：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># for file in `ls`;do echo $file;done</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/for%20%E5%BE%AA%E7%8E%AF/03.png?raw=true" alt=""></figure></p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell/12. shell中的select用法/">shell中的select用法</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell/">shell</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;select也是循环的一种，它比较适合用在用户选择的情况下。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;比如，我们有一个这样的需求，运行脚本后，让用户去选择数字，选择1，会运行w命令，选择2运行top命令，选择3运行free命令，选择4退出。脚本这样实现：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"</span></div><div class="line"><span class="built_in">echo</span></div><div class="line">select <span class="built_in">command</span> <span class="keyword">in</span> w top free quit</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="keyword">case</span> <span class="variable">$command</span> <span class="keyword">in</span></div><div class="line">    w)</div><div class="line">        w</div><div class="line">        ;;</div><div class="line">    top)</div><div class="line">        top</div><div class="line">        ;;</div><div class="line">    free)</div><div class="line">        free</div><div class="line">        ;;</div><div class="line">    quit)</div><div class="line">        <span class="built_in">exit</span></div><div class="line">        ;;</div><div class="line">    *)</div><div class="line">        <span class="built_in">echo</span> <span class="string">"Please input a number:(1-4)."</span></div><div class="line">        ;;</div><div class="line">    <span class="keyword">esac</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行结果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">sh select.sh</div><div class="line">Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit</div><div class="line"></div><div class="line">1) w</div><div class="line">2) top</div><div class="line">3) free</div><div class="line">4) quit</div><div class="line"><span class="comment">#? 1</span></div><div class="line">16:03:40 up 32 days,  2:42,  1 user,  load average: 0.01, 0.08, 0.08</div><div class="line">USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT</div><div class="line">root     pts/0    61.135.172.68    15:33    0.00s  0.02s  0.00s sh select.sh</div><div class="line"></div><div class="line"><span class="comment">#? 3</span></div><div class="line">             total       used       free     shared    buffers     cached</div><div class="line">Mem:       1020328     943736      76592          0      86840     263624</div><div class="line">-/+ buffers/cache:     593272     427056</div><div class="line">Swap:      2097144      44196    2052948</div><div class="line"><span class="comment">#?</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们发现，select会默认把序号对应的命令列出来，每次输入一个数字，则会执行相应的命令，命令执行完后并不会退出脚本。它还会继续让我们再次输如序号。序号前面的提示符，我们也是可以修改的，利用变量PS3即可，再次修改脚本如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">PS3=<span class="string">"Please select a number: "</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"</span></div><div class="line"><span class="built_in">echo</span></div><div class="line">select <span class="built_in">command</span> <span class="keyword">in</span> w top free quit</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="keyword">case</span> <span class="variable">$command</span> <span class="keyword">in</span></div><div class="line">    w)</div><div class="line">        w</div><div class="line">        ;;</div><div class="line">    top)</div><div class="line">        top</div><div class="line">        ;;</div><div class="line">    free)</div><div class="line">        free</div><div class="line">        ;;</div><div class="line">    quit)</div><div class="line">        <span class="built_in">exit</span></div><div class="line">        ;;</div><div class="line">    *)</div><div class="line">        <span class="built_in">echo</span> <span class="string">"Please input a number:(1-4)."</span></div><div class="line">    <span class="keyword">esac</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果想要脚本每次输入一个序号后就自动退出，则需要再次更改脚本如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">PS3=<span class="string">"Please select a number: "</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"</span></div><div class="line"><span class="built_in">echo</span></div><div class="line">select <span class="built_in">command</span> <span class="keyword">in</span> w top free quit</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="keyword">case</span> <span class="variable">$command</span> <span class="keyword">in</span></div><div class="line">    w)</div><div class="line">        w;<span class="built_in">exit</span></div><div class="line">        ;;</div><div class="line">    top)</div><div class="line">        top;<span class="built_in">exit</span></div><div class="line">        ;;</div><div class="line">    free)</div><div class="line">        free;<span class="built_in">exit</span></div><div class="line">        ;;</div><div class="line">    quit)</div><div class="line">        <span class="built_in">exit</span></div><div class="line">        ;;</div><div class="line">    *)</div><div class="line">        <span class="built_in">echo</span> <span class="string">"Please input a number:(1-4)."</span>;<span class="built_in">exit</span></div><div class="line">    <span class="keyword">esac</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell/10. if 判断的几种用法/">if 判断的几种用法</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell/">shell</a>
			</span>
		
	</div>

	

	
		<h2 id="1-和文档相关的判断"><a href="#1-和文档相关的判断" class="headerlink" title="1.和文档相关的判断"></a>1.和文档相关的判断</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;shell 脚本中 if 还经常判断关于档案属性，比如判断是普通文件还是目录，判断文件是否有读写执行权限等。常用的也就几个选项：</p>
<ul>
<li>-e：判断文件或目录是否存在；</li>
<li>-d：判断是不是目录，并是否存在；<br>-f：判断是否是普通文件，并存在；</li>
<li>-r：判断文档是否有读权限；</li>
<li>-w：判断文档是否有写权限；</li>
<li>-x：判断是否可执行。</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用 if 判断时，具体格式为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> [ -e filename ] ;<span class="keyword">then</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例子:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># if [ -d /home/ ];then echo ok;fi</span></div><div class="line">ok</div><div class="line">[root@192 sbin]<span class="comment"># if [ -f /home/ ];then echo ok;fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;因为 /home/ 为目录非文件，所以 -f 并不会显示 “ok”。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># if [ -f /home/1.txt ];then echo ok;fi </span></div><div class="line">ok</div><div class="line">[root@192 sbin]<span class="comment"># if [ -r /home/1.txt ];then echo ok;fi</span></div><div class="line">ok</div><div class="line">[root@192 sbin]<span class="comment"># if [ -w /home/1.txt ];then echo ok;fi</span></div><div class="line">ok</div><div class="line">[root@192 sbin]<span class="comment"># if [ -x /home/1.txt ];then echo ok;fi</span></div><div class="line">[root@192 sbin]<span class="comment"># if [ -e /home/1.txt ];then echo ok;fi</span></div><div class="line">ok</div></pre></td></tr></table></figure>
<h2 id="2-变量是否为空"><a href="#2-变量是否为空" class="headerlink" title="2.变量是否为空"></a>2.变量是否为空</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有时候需要判断一个变量的值是否为空，以避免后续操作产生异常。如果不去判断变量是否有值，就接着在后续命令中引用该变量，则会出错。比如下面脚本：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim 3.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">n=`wc -l 1.txt|awk <span class="string">'&#123;print $1&#125;'</span>`</div><div class="line"><span class="keyword">if</span> [ <span class="variable">$n</span> -gt 10 ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"The file 1.txt has more than 10 lines."</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个脚本看上去是没有问题，但没有考虑到 1.txt 文件不存在的情况，如果文件不存在，那么 n 的值也是不存在的。后面的判断也会出错。所以应该先判断一下 n 是否为空。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用下面方法即可：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim 4.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">a=</div><div class="line"><span class="keyword">if</span> [ -n <span class="string">"<span class="variable">$a</span>"</span> ]</div><div class="line"><span class="keyword">then</span></div><div class="line">   <span class="built_in">echo</span> <span class="string">"a is not null."</span></div><div class="line"><span class="keyword">else</span></div><div class="line">   <span class="built_in">echo</span> <span class="string">"a is null."</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/if%20%E5%88%A4%E6%96%AD%E7%9A%84%E5%87%A0%E7%A7%8D%E7%94%A8%E6%B3%95/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-n 选项可以判断一个变量是否不为空，注意一定要把变量引起来，上例用双引号把 $a 引起来了，否则是不对的，还有一个和 -n 正好相对的， -z ，用法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim 5.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">a=</div><div class="line"><span class="keyword">if</span> [ -z <span class="variable">$a</span> ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"a is null."</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/if%20%E5%88%A4%E6%96%AD%E7%9A%84%E5%87%A0%E7%A7%8D%E7%94%A8%E6%B3%95/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个 -z 后面的 $a 可以不用双引号引起来。</p>
<h2 id="3-if-判断条件可以是一条命令"><a href="#3-if-判断条件可以是一条命令" class="headerlink" title="3.if 判断条件可以是一条命令"></a>3.if 判断条件可以是一条命令</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># if grep -q '^yanyi:' /etc/passwd;then echo "user yanyi exist.";fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;grep -p 选项的作用是，过滤但不输出。用在 if 判断中，不需要输出结果，只需要知道它到底有没有执行成功，也就是说如果 /etc/passwd 文件中含有 yanyi 这个用户，那么条件就成立了。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/if%20%E5%88%A4%E6%96%AD%E7%9A%84%E5%87%A0%E7%A7%8D%E7%94%A8%E6%B3%95/03.png?raw=true" alt=""></figure></p>

	

	

</article>





	<span class="different-posts">📖 <a href="/page/15">more posts</a> 📖</span>



	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
